/****************************************************************************
 *
 *   Copyright (c) 2025 H743 Flight Control Project. All rights reserved.
 *   Adapted from PX4 Autopilot Project
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/**
 * @file defines.h
 * 
 * PX4 Platform Common Defines for FreeRTOS
 * 
 * This file provides common definitions and macros for the PX4 platform
 * layer running on FreeRTOS.
 */

#ifndef __PX4_PLATFORM_COMMON_DEFINES_H
#define __PX4_PLATFORM_COMMON_DEFINES_H

#ifdef __cplusplus
extern "C" {
#endif

/* Standard includes */
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>

/* FreeRTOS includes */
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "timers.h"

/* Platform identification */
#define __PX4_FREERTOS  1

/* Time conversion macros */
#define USEC_PER_SEC    1000000ULL
#define USEC_PER_MSEC   1000ULL
#define NSEC_PER_SEC    1000000000ULL
#define NSEC_PER_USEC   1000ULL

/* Utility macros */
#ifndef UNUSED
#define UNUSED(x) ((void)(x))
#endif

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#endif

#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif

#ifndef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif

/* Compiler attributes */
#ifndef __EXPORT
#define __EXPORT
#endif

#ifndef __BEGIN_DECLS
#ifdef __cplusplus
#define __BEGIN_DECLS extern "C" {
#define __END_DECLS }
#else
#define __BEGIN_DECLS
#define __END_DECLS
#endif
#endif

/* Debug macros - will be implemented later */
#define PX4_DEBUG(fmt, ...)   do { } while(0)
#define PX4_INFO(fmt, ...)    do { } while(0)
#define PX4_WARN(fmt, ...)    do { } while(0)
#define PX4_ERR(fmt, ...)     do { } while(0)

/* OK and ERROR codes */
#ifndef OK
#define OK 0
#endif

#ifndef ERROR
#define ERROR -1
#endif

/* Boolean values */
#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

/* Null pointer */
#ifndef NULL
#define NULL ((void *)0)
#endif

/* Critical section macros for FreeRTOS */
#define PX4_ENTER_CRITICAL() taskENTER_CRITICAL()
#define PX4_EXIT_CRITICAL()  taskEXIT_CRITICAL()

/* Interrupt-safe critical section */
#define PX4_ENTER_CRITICAL_FROM_ISR() UBaseType_t uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR()
#define PX4_EXIT_CRITICAL_FROM_ISR()  taskEXIT_CRITICAL_FROM_ISR(uxSavedInterruptStatus)

/* Memory barriers */
#define PX4_MEMORY_BARRIER() __asm__ __volatile__("" ::: "memory")

/* Atomic operations - basic implementation */
typedef volatile uint32_t px4_atomic_t;

static inline uint32_t px4_atomic_fetch_add(px4_atomic_t *ptr, uint32_t val)
{
    PX4_ENTER_CRITICAL();
    uint32_t old = *ptr;
    *ptr += val;
    PX4_EXIT_CRITICAL();
    return old;
}

static inline uint32_t px4_atomic_fetch_sub(px4_atomic_t *ptr, uint32_t val)
{
    PX4_ENTER_CRITICAL();
    uint32_t old = *ptr;
    *ptr -= val;
    PX4_EXIT_CRITICAL();
    return old;
}

#ifdef __cplusplus
}
#endif

#endif /* __PX4_PLATFORM_COMMON_DEFINES_H */

